<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-row :gutter="24">
        <el-col :md="6" :sm="12">
          <el-form-item label="门店名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入门店名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item label="门店电话" prop="shopTel">
            <el-input
              v-model="queryParams.shopTel"
              placeholder="请输入门店电话"
              clearable
              style="width: 140px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item label="门店地址" prop="address">
            <el-input
              v-model="queryParams.address"
              placeholder="请输入门店地址"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item style="margin-bottom: 20px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <a style="margin-left: 8px" @click="toggleAdvanced">
              {{ advanced ? '收起' : '展开' }}
              <el-icon :class="advanced?'el-icon-arrow-up':'el-icon-arrow-down'"/>
            </a></el-form-item>
        </el-col>
        <el-collapse-transition>
          <div v-show="advanced">
            <el-col :md="6" :sm="12">
              <el-form-item label="请输入所在省份" prop="province">
                <el-select
                  v-model="queryParams.province"
                  placeholder="请输入所在省份"
                  clearable
                  @keyup.enter.native="handleQuery"
                  @change="getCityListOption"
                  style="width: 130px;">
                  <el-option v-for="item in provinceOption" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="请输入所在城市" prop="city">
                <el-select
                  v-model="queryParams.city"
                  placeholder="请输入所在城市"
                  clearable
                  @keyup.enter.native="handleQuery"
                  @change="getAreaListOption"
                  style="width: 130px;">
                  <el-option v-for="item in cityOption" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="请输入所在区域" prop="area">
                <el-select
                  v-model="queryParams.area"
                  placeholder="请输入所在区域"
                  clearable
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option v-for="item in areaOption" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="门店状态" prop="status">
                <el-select
                  v-model="queryParams.status"
                  placeholder="请选择门店状态"
                  clearable
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="是否上面送车" prop="status">
                <el-select
                  v-model="queryParams.ranges"
                  placeholder="请选择是否上面送车"
                  clearable
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option
                    v-for="item in shopRangesOption"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </div>
        </el-collapse-transition>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stores:branch:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stores:branch:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stores:branch:add']"
        >导出
        </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <el-table v-loading="loading" lazy :data="storeList" row-key="id" :row-class-name="tableRowClassName"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="门店图片" align="center" prop="pic">
        <template slot-scope="scope">
          <img v-bind:src="scope.row.pic" class="img-circle-shop img-lg-shop"/>
        </template>
      </el-table-column>
      <el-table-column label="门店id" align="center" prop="id"/>
      <el-table-column label="门店名称" align="center" prop="name" width="200"/>
      <el-table-column label="门店地址" align="center" prop="address"/>
      <el-table-column label="门店联系人" align="center" prop="linkman"/>
      <el-table-column label="门店营业时间" align="center" prop="businessHours"/>
      <el-table-column label="门店电话" align="center" prop="shopTel"/>
      <el-table-column label="门店状态" align="center" prop="status">
        <template v-slot:default="scope">
          {{ getStatusLabel(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" width="200">
        <template v-slot:default="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['stores:branch:update']"
          >修改
          </el-button>
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="handleBrancherUser"
            v-hasPermi="['stores:branch:update']"
          >用户管理
          </el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
                       v-hasPermi="['system:user:resetPwd', 'system:user:edit']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleBrancher" icon="el-icon-key"
                                v-hasPermi="['stores:branch:auth']">任命店长
              </el-dropdown-item>
              <el-dropdown-item
                command="handleDelete(scope.row)"
                icon="el-icon-delete"
                size="mini"
                v-hasPermi="['stores:branch:delete']"
              >删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <add-store ref="addStore" :status-options="statusOptions" :shop-ranges-option="shopRangesOption"/>
  </div>
</template>
<script>
import {getCityList} from "@/api/option";
import {delStores, storesRoleList} from "@/api/merchant/stores";
import addStore from './computed/addStore.vue'
import {listData} from "@/api/system/dict/data";

export default {
  name: "index",
  components: {addStore},
  data() {
    return {
      total: 0,
      ids: [],
      single: false,
      multiple: true,
      statusOptions: [{
        value: '营业',
        label: 1
      }, {
        value: '关闭',
        label: 0
      }],
      shopRangesOption: [],
      loading: true,
      storeList: [],
      provinceOption: [],
      cityOption: [],
      areaOption: [],
      // 显示搜索
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ranges: '',        //是否上面送车
        address: '',       //门店地址
        name: '',         //门店名称
        remark: '',      //备注信息
        province: '',    //所在省份
        city: '',        //所在城市
        area: '',        //所在区域
        status: '',      // 门店状态
        shopTel: '',      //门店手机号
        linkman: '',       //联系人
      },
      advanced: false
    }
  },
  methods: {
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      console.log(this.ids)
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    getStatusLabel(dictValue) {
      for (let i = 0; i < this.statusOptions.length; i++) {
        if (this.statusOptions[i].dictValue == dictValue) {
          return this.statusOptions[i].dictLabel
        }
      }
    },
    tableRowClassName({row, rowIndex}) {
      if (!row.address || row.address == '') {
        return 'warning-row';
      }
      if (!row.shopTel || row.shopTel == '')
        return 'warning-row';
      if (!row.linkman || row.linkman == '') {
        return 'warning-row';
      }
      if (!row.businessHours || row.businessHours == '') {
        return 'warning-row';
      }
      return ''
    },
    init() {
      getCityList({pid: 0}).then(res => {
        if (res.code === 200) {
          this.provinceOption = res.data
        }
      })
      listData({
        dictType: 'shop_ranges'
      }).then(res => {
        this.shopRangesOption = res.rows
      })
      listData({
        dictType: 'shop_status'
      }).then(res => {
        this.statusOptions = res.rows
      })
      this.getList()
    },
    getCityListOption(pid) {
      getCityList({pid: pid}).then(res => {
        if (res.code === 200) {
          this.cityOption = res.data
          this.queryParams.area = ''
          this.queryParams.city = ''
        }
      }).catch(res => {

        }
      )
    },
    getAreaListOption(pid) {
      getCityList({pid: pid}).then(res => {
        if (res.code === 200) {
          this.areaOption = res.data
          this.queryParams.area = ''
        }
      }).catch(res => {
        }
      )
    },
    handleAdd() {
      this.$refs.addStore.open()
    },
    handleUpdate(item) {
      this.$refs.addStore.open(item)
    },
    handleDelete(item) {
      let storesId = item.id || this.ids
      this.$modal.confirm('是否确认删除门店编号为"' + storesId + '"的数据项？').then(function () {
        return delStores(storesId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleBrancher(item) {
      let storesId = item.id || this.ids
      this.$router.push('/stores/auth-branch/role/' + storesId);
    },
    handleBrancherUser(item) {
      let storesId = item.id || this.ids
      this.$router.push('/system/user?shopIds=' + storesId);
    },
    handleExport() {
      this.download('/shop/branch/export', {...this.queryParams}, `data_${new Date().getTime()}.xlsx`)
    },
    handleCommand(command, row) {
      switch (command) {
        case "handleBrancher":
          this.handleBrancher(row)
          break
        case "handleDelete":
          this.handleDelete(row)
          break
        default:
          break;
      }
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        ranges: '',        //是否上面送车
        address: '',       //门店地址
        name: '',         //门店名称
        remark: '',      //备注信息
        province: '',    //所在省份
        city: '',        //所在城市
        area: '',        //所在区域
        status: '',      // 门店状态
        shopTel: '',      //门店手机号
        linkman: '',       //联系人
      }
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      storesRoleList(this.queryParams).then(res => {
        this.storeList = res.rows
        this.total = res.total
        this.loading = false
      })
    }
  },
  created() {
    this.init()
  }
}
</script>
<style lang="scss">
.el-table .warning-row {
  background: oldlace !important;
}
</style>
